package com.ccjq.shosestore.shoppingcart.dao.impl;/*
 * @program: shosestore-parent
 * @description: ShopcartDao实现类
 * @author: Danst
 * @create: 2019-04-08 14:17
 * @version:1.0
 */

import com.ccjq.shosestore.core.dbmanager.DbConnection;
import com.ccjq.shosestore.shoppingcart.bo.ShopcartBo;
import com.ccjq.shosestore.shoppingcart.dao.ShopcartDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ShopcartDaoImpl implements ShopcartDao {
    Connection conn = null;
    PreparedStatement st = null;
    ResultSet re = null;

    public List<ShopcartBo> inserShopcart() {
        List<ShopcartBo> list = new ArrayList<ShopcartBo>();
        String sql = "insert into ";
        return list;
    }

    /**
     * 根据商品ID 查询库存量
     *
     * @param prid
     * @return
     */
    public boolean selectstock(String prid) {

        boolean b;
        String sql = "select * from  t_stock where product_id=? and quantity>0";
        conn = DbConnection.getConnextion();
        try {
            st = conn.prepareStatement(sql);
            st.setString(1, prid);
            re = st.executeQuery();
            if (re.next()) {
                return b = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbConnection.close(re, st, conn);
        }
        return false;
    }

    public List<ShopcartBo> insertCar(String carid, String prid, int quantity, String userid) {
        ShopcartBo sh = new ShopcartBo();
        List<ShopcartBo> list = new ArrayList<ShopcartBo>();
        String sql = null;
        if (quantity == 1) {
            sql = "insert into t_shopping_cart values(?,?,?,?)";
        } else {
            sql = "insert into t_shopping_cart values(?,?,?,?)";
        }
        conn = DbConnection.getConnextion();
        try {
            st = conn.prepareStatement(sql);
            st.setString(1, carid);
            st.setString(2, prid);
            st.setInt(3, quantity);
            st.setString(4, userid);
            st.executeUpdate();
            sh.setShopping_cart_id(carid);
            sh.setProduct_id(prid);
            sh.setQuantity(quantity);
            sh.setUser_id(userid);

            list.add(sh);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbConnection.close(re, st, conn);
        }
        return list;
    }

    public List<ShopcartBo> selectCar(String userid) {
        ShopcartBo sh;
        List<ShopcartBo> list = new ArrayList<ShopcartBo>();
        String sql = "select shopping_cart_id,a.product_id,quantity,user_id,feature,imgpath,price from t_shopping_cart a join t_product b on a.product_id=b.product_id where user_id=?";
        conn = DbConnection.getConnextion();
        try {
            st = conn.prepareStatement(sql);
            st.setString(1, userid);
            re = st.executeQuery();
            while (re.next()) {
                sh = new ShopcartBo();
                sh.setShopping_cart_id(re.getString(1));
                sh.setProduct_id(re.getString(2));
                sh.setQuantity(re.getInt(3));
                sh.setUser_id(re.getString(4));
                sh.setFeature(re.getString(5));
                sh.setImgpath(re.getString(6));
                sh.setPrice(re.getInt(7));
                list.add(sh);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbConnection.close(re, st, conn);
        }
        return list;
    }

    public void deleteCar(String carid) {

        String sql = "delete from t_shopping_cart where shopping_cart_id=?";
        conn = DbConnection.getConnextion();
        try {
            st = conn.prepareStatement(sql);
            st.setString(1, carid);
            st.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbConnection.close(re, st, conn);
        }
    }

    public boolean selectOldqu(String prid, int quntity) {
        String sql = "select quantity from t_shopping_cart where product_id=?";
        boolean b=true;
        conn = DbConnection.getConnextion();
        try {
            st = conn.prepareStatement(sql);
            st.setString(1, prid);
            re = st.executeQuery();
            if (re.next()) {
                int oldqu = re.getInt("quantity");
                if (oldqu>=quntity) {
                    return b;
                }
            }
            re.getInt("quantity");
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbConnection.close(re, st, conn);
        }
        return false;
    }

    public void update(String prid,String usrid,int quantity) {

        String sql = "update t_shopping_cart set quantity=? where  product_id=? and user_id=?";
        conn = DbConnection.getConnextion();
        try {
            st = conn.prepareStatement(sql);
            st.setInt(1, quantity);
            st.setString(2, prid);
            st.setString(3,usrid);
            st.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbConnection.close(null, st, conn);
        }
    }
}
